Imports Microsoft.VisualBasic

Public Class BLL_SearchLoginRecord
    Public Function GetData(ByVal startingLogindate As Date, ByVal endingLoginDate As Date, ByVal sort As String) As Data.DataTable
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("select lr.*, UserName = case when ISNULL(u.UserID,'')='' then lr.UserID else u.UserName end ")
        sql.AppendFormat(", SuccessText = cm_Success.Text ")
        sql.AppendFormat("from LoginRecord lr ")
        sql.AppendFormat("left join [User] u on lr.UserID = u.UserID ")
        sql.AppendFormat("inner join CodeMapping cm_Success on lr.Success = cm_Success.Value and cm_Success.TableName = 'LoginRecord' and cm_Success.ColumnName = 'Success' ")
        sql.AppendFormat("where 1=1 ")

        sql.AppendFormat("and lr.LoginDate between @StartingLoginDate and @EndingLoginDate ")
        sql.AddWithValue("StartingLoginDate", startingLogindate)
        sql.AddWithValue("EndingLoginDate", endingLoginDate)

        If String.IsNullOrEmpty(sort) = True Then
            sql.AppendFormat("order by LoginDate desc ")
        Else
            sql.AppendFormat("order by " + sort)
        End If
        'MyObj.WriteLog(sort + " aa")
        Return dat.GetData(sql)
    End Function

    Public Sub WriteLoginRecord(ByVal userId As String, ByVal success As Boolean)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope
            sql.Length = 0
            sql.AppendFormat("insert LoginRecord(UserID, LoginDate, Success) select ")
            sql.AppendFormat(" UserID = '{0}' ", userId)
            sql.AppendFormat(",LoginDate = getdate() ", userId)
            sql.AppendFormat(",Success = '{0}' ", IIf(success = True, "Y", "N"))

            dat.AccData(sql)

            scope.Complete()
        End Using
    End Sub
End Class
